﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using vtech_formBase;
using vtech_frameWorks.Common;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Library;
using vtech_frameWorks.Library.Encryption;

namespace vtech_system.User
{
    public partial class frmEditUser : frmEdit
    {
        #region Delegate
        public delegate void EditDelegate();
        public event EditDelegate OnEdit = null;

        protected override void FireEvent()
        {
            if (this.OnEdit != null)
            {
                this.OnEdit();
            }
        }

        #endregion

        #region Constructor
        public frmEditUser()
        {
            this.InitializeComponent();
        }

        public frmEditUser(Guid ID)
        {
            this.InitializeComponent();
            this.ID = ID;
        }

        #endregion

        protected override void GetData()
        {
            ClsUserDAO obj = new ClsUserDAO();
            DataRow[] drow;
            drow = obj.GetAll().Select(string.Format("ID='{0}'", this.ID));
            foreach (DataRow row in drow)
            {
                this.txtUserName.Text = row["UserName"].ToString();
                this.txtPassword.Text = ClsMd5.Decrypt(row["Password"].ToString(), ClsGlobal.strPrivateKey, true);
                this.txtRePassword.Text = ClsMd5.Decrypt(row["Password"].ToString(), ClsGlobal.strPrivateKey, true);
                this.txtFullName.Text = row["FullName"].ToString();
                this.txtDescription.Text = row["Description"].ToString();
                this.txtEmail.Text = row["Email"].ToString();
                this.txtWebsite.Text = row["Website"].ToString();
                this.txtWorkPhone.Text = row["WorkPhone"].ToString();
                this.txtMobilePhone.Text = row["MobilePhone"].ToString();
                this.txtHomePhone.Text = row["MobilePhone"].ToString();
                this.txtFax.Text = row["Fax"].ToString();
                this.txtWorkAddress.Text = row["WorkAddress"].ToString();
                this.txtHomeAddress.Text = row["HomeAddress"].ToString();
                if (row["Photo"].ToString() != "")
                {
                    this.pbAvatar.Image = ClsImages.ByteArrayToImage((byte[])row["Photo"]);
                }
                else
                {
                    this.pbAvatar.Image = global::vtech_system.Properties.Resources.Avatar;
                }
                if (row["Inactive"].ToString().Equals("True"))
                {
                    this.chkActive.Checked = true;
                }
                else
                {
                    this.chkActive.Checked = false;
                }
            }
        }

        protected override bool CheckControl()
        {
            bool flag = false;
            try
            {
                this.errorProvider.Dispose();
                if (this.txtUserName.Text == "")
                {
                    this.errorProvider.SetError(this.txtUserName, "Bạn chưa nhập tên đăng nhập. Xin vui lòng kiểm tra lại");
                    flag = false;
                }
                else
                {
                    if (this.txtPassword.Text == "")
                    {
                        this.errorProvider.SetError(this.txtPassword, "Bạn chưa nhập mật khẩu. Xin vui lòng kiểm tra lại");
                        flag = false;
                    }
                    else
                    {
                        if (!this.txtPassword.Text.Trim().Equals(this.txtRePassword.Text.Trim()))
                        {
                            this.errorProvider.SetError(this.txtPassword, "Mật khẩu không khớp. Xin vui lòng kiểm tra lại");
                            flag = false;
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                flag = false;
            }
            return flag;
        }

        protected override void Edit()
        {
            ClsUserBS obj = new ClsUserBS();
            try
            {
                using (DsUser ds = new DsUser())
                {
                    DataRow drow = ds.DtUser.NewRow();
                    ds.DtUser.Rows.Add(drow);
                    drow["UserName"] = this.txtUserName.Text.Trim();
                    drow["Password"] = ClsMd5.Encrypt(this.txtPassword.Text.Trim(), ClsGlobal.strPrivateKey, true);
                    drow["FullName"] = this.txtFullName.Text.Trim();
                    drow["Description"] = this.txtDescription.Text.Trim();
                    drow["Email"] = this.txtEmail.Text.Trim();
                    drow["Website"] = this.txtWebsite.Text.Trim();
                    drow["WorkPhone"] = this.txtWorkPhone.Text.Trim();
                    drow["HomePhone"] = this.txtHomePhone.Text.Trim();
                    drow["MobilePhone"] = this.txtMobilePhone.Text.Trim();
                    drow["Fax"] = this.txtFax.Text.Trim();
                    drow["WorkAddress"] = this.txtWorkAddress.Text.Trim();
                    drow["HomeAddress"] = this.txtHomeAddress.Text.Trim();
                    drow["Photo"] = ClsImages.ImageToByteArray(this.pbAvatar.Image);
                    drow["Inactive"] = 0;
                    drow["IsOnline"] = 0;
                    if (this.chkActive.Checked == true)
                    {
                        drow["Inactive"] = 1;
                    }
                    else
                    {
                        drow["Inactive"] = 0;
                    }
                    ds.DtUser.Rows.Add(drow);
                    obj.DoEdit(ds.DtUser);
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        private void frmEditUser_Load(object sender, EventArgs e)
        {
            base.frmEdit_Load(sender, e);
        }
    }
}